package com.itheima.edu.day25DataGramDemo.day25_1UUID.test;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadSocket implements Runnable{
    private Socket threadAccept;
    public ThreadSocket(Socket accept) {
        this.threadAccept = accept;
    }

    @Override
    public void run() {

        //创建输入流，从网络读取数据到内存中
        BufferedOutputStream bos = null;
        try {
            InputStream is = threadAccept.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            //将网络输入流变成本地输出流，将数据写入硬盘中
            bos = new BufferedOutputStream(new FileOutputStream("D:\\Develop\\javaCode\\itheima-edu-info-manager\\src\\com\\itheima\\edu\\day25DataGramDemo\\day25_1UUID\\test\\"+ UUID.randomUUID().toString()+".jpg"));

            //写入数据
            int b;
            while ((b = bis.read())!= -1){
                bos.write(b);
            }

            OutputStream os = threadAccept.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write("上传成功！");
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos!= null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(threadAccept != null){
                try {
                    threadAccept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
